"use strict";
var Premium = Premium || {};

Premium.creative = {
    init: function() {
        /* START OF CUSTOM JS */

        Premium.communicator.init(document.body.id === "body_expanded" ? undefined : 4);

        switch (document.body.id) {
            case "body_top":
                // TOP ---------------------------------------------------------------

                Premium.creative.createVideoPlayerAppended = function(video, container) {
                    Premium.video.sync(video, 3);
                    var prom = video.play(); // Explicitly play the video
                    if (prom) {
                        prom.catch(function() {})
                    }
                    PremiumJpControls.callOnClickFullScreen(function(el) {
                        video.muted = false;
                        Premium.expand.expand("expanded.html", "width:100%;height:100%;")
                    })
                }

                break;

            case "body_left":
            case "body_right":

                var videoContainerEl = document.querySelector(".video-container");
                var scrollPos = "up";

                var checkShowVideo = function() {
                    gsap.set(videoContainerEl, {autoAlpha:scrollPos==="down" ? 1 : 0})
                }

                var playerAppendedHandler = function(){
                    var video = videoContainerEl.querySelector("VIDEO");
                    video.volume = 0;
                    Premium.video.sync(video, 3, Premium.video.SyncType_Get);
                    video.play().catch(function(e) {});
                    PremiumJpControls.callOnClickFullScreen(function(el) {
                        video.muted = false;
                        Premium.expand.expand("expanded.html", "width:100%;height:100%;")
                    })
                    checkShowVideo();
                }

                Premium.video.switchOnScroll(undefined, 3, function(pos) {
                    scrollPos = pos;
                    if (scrollPos === "down") {
                        if (!videoContainerEl.querySelector("VIDEO")) {
                            Premium.jpxApi.createVideo(document.body.id, videoContainerEl, { src: "https://video-content.gumgum.com/edr-677-emea-adidas-ss23running-fr_out/7320438595509082275/edr-677-emea-adidas-ss23running-fr_out_2.mp4", poster: "assets/poster.jpg", type: "video/mp4" }, "showfullscreen");
                            Premium.jpxApi.videoEvent("playerAppended", document.body.id, playerAppendedHandler);
                        }
                    }
                    checkShowVideo();
                });

                break;

            case "body_expanded":
                // EXPANDED ------------------------------------------------------------

                Premium.creative.createVideoPlayerAppended = function(video, container) {
                    video.volume = 0;
                    video.addEventListener("playing", function() {
                        setTimeout(function() {
                            document.body.className += " fade-in";
                        }, 100);
                    }, { once: true })
                    Premium.video.sync(video, undefined, Premium.video.SyncType_Get);
                    var prom = video.play(); // Explicitly play the video
                    if (prom) {
                        prom.catch(function() {})
                    }

                    // Safari fix
                    document.body.addEventListener("click", function(e) {
                        if (e.target.className && (e.target.className.indexOf("jp-controls-play") > -1 || e.target.className.indexOf("jp-controls-bigplay") > -1)) {
                            if (!video.paused) {
                                setTimeout(function() {
                                    video.play();
                                }, 1000)

                            }
                        }
                    });

                }
                Premium.creative.createVideoPlayerAppended(document.querySelector("VIDEO"));
                
                break;

        }
        /* END OF CUSTOM JS */
    }
}